package com.itlong.cloud.config.filelistener;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.PO.DeviceRunLogPO;
import com.itlong.cloud.equip.commons.ICloudDeviceBaseServiceClient;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.random.IdWorker;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;


@Component
public class UploadErrorListener{

    @Autowired
    private ICloudDeviceBaseServiceClient iCloudDeviceBaseService;

    @Autowired
    RedisService redisService;

    final IdWorker idWorker = new IdWorker(18);
    @EventListener
    @Async
    public void listener(UploadErrorEvent event) throws Exception {
        DeviceRunLogPO deviceRunLogPO = JSON.parseObject(event.getMsg(), DeviceRunLogPO.class);
        deviceRunLogPO.setDeviceType(StringHandlerUtil.getDeviceTypeByDeviceUnique(deviceRunLogPO.getDeviceUnique()));
        deviceRunLogPO.setUploadStatus(-1);
        deviceRunLogPO.setStatus(1);
        deviceRunLogPO.setRunLogId(idWorker.nextId() + "");
        deviceRunLogPO.setCreateTime(new Date());
        deviceRunLogPO.setFileSize(0L);
        iCloudDeviceBaseService.saveDeviceRunLog(deviceRunLogPO);
        redisService.del("deviceUnique");
    }

}
